use cargo::core::source::{Source, SourceId, GitReference};
use cargo::sources::git::{GitSource};
-use cargo::util::{Config, CliResult, CliError, human, ToUrl};
+use cargo::util::{Config, CliResult, ToUrl};
#[derive(RustcDecodable)]
pub struct Options {
options.flag_locked));
let Options { flag_url: url, flag_reference: reference, .. } = options;
- let url = try!(url.to_url().map_err(|e| {
- human(format!("The URL `{}` you passed was \
- not a valid URL: {}", url, e))
- })
- .map_err(|e| CliError::new(e, 1)));
+ let url = try!(url.to_url());
let reference = GitReference::Branch(reference.clone());
let source_id = SourceId::for_git(&url, reference);
let mut source = GitSource::new(&source_id, config);
- try!(source.update().map_err(|e| {
- CliError::new(human(format!("Couldn't update {:?}: {:?}", source, e)), 1)
- }));
+ try!(source.update());
Ok(None)
}
use cargo::ops;
use cargo::core::{SourceId, GitReference};
-use cargo::util::{CliResult, Config, ToUrl, human};
+use cargo::util::{CliResult, Config, ToUrl};
#[derive(RustcDecodable)]
pub struct Options {
};
let source = if let Some(url) = options.flag_git {
- let url = try!(url.to_url().map_err(human));
+ let url = try!(url.to_url());
let gitref = if let Some(branch) = options.flag_branch {
GitReference::Branch(branch)
} else if let Some(tag) = options.flag_tag {
fn decode<D: Decoder>(d: &mut D) -> Result<PackageId, D::Error> {
let string: String = try!(Decodable::decode(d));
let regex = Regex::new(r"^([^ ]+) ([^ ]+) \(([^\)]+)\)$").unwrap();
- let captures = regex.captures(&string).expect("invalid serialized PackageId");
+ let captures = try!(regex.captures(&string).ok_or_else(|| {
+ d.error("invalid serialized PackageId")
+ }));
let name = captures.at(1).unwrap();
let version = captures.at(2).unwrap();
let url = captures.at(3).unwrap();
- let version = semver::Version::parse(version).ok().expect("invalid version");
- let source_id = SourceId::from_url(url);
+ let version = try!(semver::Version::parse(version).map_err(|_| {
+ d.error("invalid version")
+ }));
+ let source_id = try!(SourceId::from_url(url).map_err(|e| {
+ d.error(&e.to_string())
+ }));
Ok(PackageId {
inner: Arc::new(PackageIdInner {
fn decode<D: Decoder>(d: &mut D) -> Result<EncodablePackageId, D::Error> {
let string: String = try!(Decodable::decode(d));
let regex = Regex::new(r"^([^ ]+) ([^ ]+)(?: \(([^\)]+)\))?$").unwrap();
- let captures = regex.captures(&string)
- .expect("invalid serialized PackageId");
+ let captures = try!(regex.captures(&string).ok_or_else(|| {
+ d.error("invalid serialized PackageId")
+ }));
let name = captures.at(1).unwrap();
let version = captures.at(2).unwrap();
- let source = captures.at(3);
-
- let source_id = source.map(|s| SourceId::from_url(s));
+ let source_id = match captures.at(3) {
+ Some(s) => {
+ Some(try!(SourceId::from_url(s).map_err(|e| {
+ d.error(&e.to_string())
+ })))
+ }
+ None => None,
+ };
Ok(EncodablePackageId {
name: name.to_string(),
/// libssh2-static-sys#80e71a3021618eb05\
/// 656c58fb7c5ef5f12bc747f");
/// ```
- pub fn from_url(string: &str) -> SourceId {
+ pub fn from_url(string: &str) -> CargoResult<SourceId> {
let mut parts = string.splitn(2, '+');
let kind = parts.next().unwrap();
let url = parts.next().unwrap();
match kind {
"git" => {
- let mut url = url.to_url().unwrap();
+ let mut url = try!(url.to_url());
let mut reference = GitReference::Branch("master".to_string());
for (k, v) in url.query_pairs() {
match &k[..] {
let precise = url.fragment().map(|s| s.to_owned());
url.set_fragment(None);
url.set_query(None);
- SourceId::for_git(&url, reference).with_precise(precise)
- }
+ Ok(SourceId::for_git(&url, reference).with_precise(precise))
+ },
"registry" => {
- let url = url.to_url().unwrap();
- SourceId::new(Kind::Registry, url)
- .with_precise(Some("locked".to_string()))
+ let url = try!(url.to_url());
+ Ok(SourceId::new(Kind::Registry, url)
+ .with_precise(Some("locked".to_string())))
}
"path" => {
- let url = url.to_url().unwrap();
- SourceId::new(Kind::Path, url)
+ let url = try!(url.to_url());
+ Ok(SourceId::new(Kind::Path, url))
}
- _ => panic!("Unsupported serialized SourceId"),
+ kind => Err(human(format!("unsupported source protocol: {}", kind)))
}
}
// Pass absolute path
pub fn for_path(path: &Path) -> CargoResult<SourceId> {
- let url = try!(path.to_url().map_err(human));
+ let url = try!(path.to_url());
Ok(SourceId::new(Kind::Path, url))
}
impl Decodable for SourceId {
fn decode<D: Decoder>(d: &mut D) -> Result<SourceId, D::Error> {
- let string: String = Decodable::decode(d).ok().expect("Invalid encoded SourceId");
- Ok(SourceId::from_url(&string))
+ let string: String = try!(Decodable::decode(d));
+ SourceId::from_url(&string).map_err(|e| {
+ d.error(&e.to_string())
+ })
}
}
} = try!(registry_configuration(config));
let token = token.or(token_config);
let index = index.or(index_config).unwrap_or(RegistrySource::default_url());
- let index = try!(index.to_url().map_err(human));
+ let index = try!(index.to_url());
let sid = SourceId::for_registry(&index);
let api_host = {
let mut src = RegistrySource::new(&sid, config);
}));
}
- let url = try!(source.to_url().map_err(human));
+ let url = try!(source.to_url());
let url = url.to_string();
let repo = try!(git2::Repository::clone(&url, into).chain_error(|| {
internal(format!("failed to clone {} into {}", source.display(),
fn fetch(&self, cargo_config: &Config) -> CargoResult<()> {
info!("fetch {}", self.repo.path().display());
- let url = try!(self.database.path.to_url().map_err(human));
+ let url = try!(self.database.path.to_url());
let url = url.to_string();
let refspec = "refs/heads/*:refs/heads/*";
try!(fetch(&self.repo, &url, refspec, &cargo_config));
pub fn url(config: &Config) -> CargoResult<Url> {
let config = try!(ops::registry_configuration(config));
let url = config.index.unwrap_or(DEFAULT.to_string());
- url.to_url().map_err(human)
+ url.to_url()
}
/// Get the default url for the registry
-use url::Url;
use std::path::Path;
+use url::Url;
+
+use util::{human, CargoResult};
+
pub trait ToUrl {
- fn to_url(self) -> Result<Url, String>;
+ fn to_url(self) -> CargoResult<Url>;
}
impl<'a> ToUrl for &'a str {
- fn to_url(self) -> Result<Url, String> {
+ fn to_url(self) -> CargoResult<Url> {
Url::parse(self).map_err(|s| {
- format!("invalid url `{}`: {}", self, s)
+ human(format!("invalid url `{}`: {}", self, s))
})
}
}
impl<'a> ToUrl for &'a Path {
- fn to_url(self) -> Result<Url, String> {
+ fn to_url(self) -> CargoResult<Url> {
Url::from_file_path(self).map_err(|()| {
- format!("invalid path url `{}`", self.display())
+ human(format!("invalid path url `{}`", self.display()))
})
}
}
.or_else(|| details.tag.clone().map(GitReference::Tag))
.or_else(|| details.rev.clone().map(GitReference::Rev))
.unwrap_or_else(|| GitReference::Branch("master".to_string()));
- let loc = try!(git.to_url().map_err(human));
+ let loc = try!(git.to_url());
SourceId::for_git(&loc, reference)
},
(None, Some(path)) => {